//
//  SEGTrackFileView.m
//  SEGWorkOrder
//
//  Created by 乔杨 on 2020/7/27.
//

#import "SEGTrackFileView.h"

@implementation SEGTrackFileView

- (instancetype)initWithFrame:(CGRect)frame dict:(NSDictionary *)dict {
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUIWithFrame:frame dict:dict];
    }
    return self;
}
/**
 UI布局
 
 @param dict  dict
 */
- (void)setupUIWithFrame:(CGRect)frame dict:(NSDictionary *)dict {
    NSString *titleStr = dict[@"title"];
    NSArray *attrValueArr = [NSJSONSerialization JSONObjectWithData:[dict[@"attrValue"] dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingAllowFragments error:nil];
//    NSArray *attrValueArr = [NSJSONSerialization JSONObjectWithData:dict[@"attrValue"] options:kNilOptions error:&error];
    __block NSString *contentStr = @"";
    if (attrValueArr.count > 0) {
        [attrValueArr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            NSDictionary *dic = attrValueArr[idx];
            if (dic[@"fileName"]) {
                if (idx == 0) {
                    contentStr = dic[@"fileName"];
                } else {
                    contentStr = [contentStr stringByAppendingFormat:@"%@", [NSString stringWithFormat:@"，%@", dic[@"fileName"]]];
                }
            }
        }];
    }
    /* 标题 */
    titleStr = [NSString stringWithFormat:@"%@:",titleStr];
    CGSize titleSize = STRING_SIZE_FONT(200, titleStr, 15);
    UILabel *titleLbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 12, titleSize.width, ceil(titleSize.height))];
    titleLbl.font = kFont_15;
    titleLbl.numberOfLines = 0;
    [titleLbl setTextColor:kColor_1];
    titleLbl.text = titleStr;
    [self addSubview:titleLbl];
    
    /* 内容 */
    UILabel *cotentLbl = [[UILabel alloc] init];
    cotentLbl.numberOfLines = 0;
    cotentLbl.font = kFont_15;
    [cotentLbl setTextColor:kColor_1];
    [cotentLbl extendAttributedString:contentStr lineSpace:6 font:kFont_15 color:kColor_1 lineBreakMode:NSLineBreakByTruncatingTail];
    CGFloat contentWidth = self.width-CGRectGetMaxX(titleLbl.frame)-20;
    CGSize contentSize = [cotentLbl textRectForBounds:CGRectMake(0, 0, contentWidth, LYScreenHeight()) limitedToNumberOfLines:0].size;
    cotentLbl.frame = CGRectMake(CGRectGetMaxX(titleLbl.frame)+5, 12, contentWidth, contentSize.height);
    [self addSubview:cotentLbl];
    
    self.height = CGRectGetMaxY(cotentLbl.frame);
}

@end
